#!/usr/bin/env pythonBasics
# -*-coding:utf-8-*-

#Author:super
import yaml
import json
from weChatApi.common.apiPublicMethod import filePath, readContent, writeContent
import os


class OpenFile():
	'''读取yaml文件'''
	def readFile(self,fileDir,fileName,types='list'):
		'''
		获取文件返回文件内容
		:param fileDir: 文件目录
		:param fileName: 文件名称
		:return:
		'''
		if os.path.splitext(filePath(fileDir=fileDir,fileName=fileName))[1] == '.yaml' and types=='list':
			with open(filePath(fileDir=fileDir,fileName=fileName),'r',encoding='utf-8') as f:
				lists = list(yaml.safe_load_all(f))
				if lists[0]['params']['access_token'] in 'token':
					lists[0]['params']['access_token']=self.readFile('data','gettoken.ini')
					return lists
				return list(yaml.safe_load_all(f))
		elif os.path.splitext(filePath(fileDir=fileDir,fileName=fileName))[1] in ['.ini','.text']:
			with open(filePath(fileDir=fileDir,fileName=fileName),'r',encoding='utf-8') as f:
				return f.read()
		elif os.path.splitext(filePath(fileDir=fileDir,fileName=fileName))[1] == '.yaml' and types=='dict':
			with open(filePath(fileDir=fileDir,fileName=fileName),'r',encoding='utf-8') as f:
				return yaml.safe_load(f)

	def  writeFile(self,fileDir,fileName,s=None):
		'''
		写入yaml文件内容
		:param fileDir: 文件路径
		:param fileName: 文件名称
		:param s: 写入数据
		:return:
		'''
		desired_caps = {
			'platformName': 'Android',
			'platformVersion': '7.0',
			'deviceName': 'A5RNW18316011440',
			'appPackage': 'com.tencent.mm',
			'appActivity': '.ui.LauncherUI',
			'automationName': 'Uiautomator2',
			'unicodeKeyboard': True,
			'resetKeyboard': True,
			'noReset': True,
			'chromeOptions': { 'androidProcess': 'com.tencent.mm:tools' }
		}
		
		# 写入到yaml文件
		# 按照顺序展示Dumper = yaml.RoundTripDumper
		with open(filePath(fileDir,fileName), "w", encoding = "utf-8") as f:
			yaml.dump(desired_caps, f, Dumper = yaml.RoundTripDumper)
		

if __name__ == '__main__':
    o = OpenFile()
    o.writeFile('data','config.yaml')